﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Speech.Synthesis;

namespace ABC
{
    public partial class Form1 : Form
    {
        private static SpeechSynthesizer speech = new SpeechSynthesizer();
        public Form1()
        {
            InitializeComponent();
            speech.Rate = -3;
            speech.Volume = 100;


            speech.StateChanged += new EventHandler<StateChangedEventArgs>(stateChangedEventArgs);
        }


        bool speekLock = false;                 

        void stateChangedEventArgs(object sender, StateChangedEventArgs e)
        {
            Console.WriteLine(e.ToString());
            if(e.State== SynthesizerState.Ready)
            {
                speekLock = false;
            }else{
                speekLock = true;
            }
        }


        private void label2_DoubleClick(object sender, EventArgs e)
        {
            this.label2.Text = char.ConvertFromUtf32(new Random().Next('A', 'Z'));
            if (speakerOn)
            {
                speech.Speak(this.label2.Text);
            }
        }

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            if (speekLock)
            {
                return;
            }

            if (keyOk == false)
            {
                this.label2.Text = char.ConvertFromUtf32(new Random().Next('A', 'Z'));
            }
            
            if (speakerOn)
            {

                speekLock = true;
                speech.Speak(this.label2.Text);
            }
        }


        bool speakerOn = true;
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            if(this.pictureBox1.Tag.ToString() == "speaker_on")
            {
                this.pictureBox1.Image = global::ABC.Properties.Resources.speaker_off;
                this.pictureBox1.Tag = "speaker_off";
                speakerOn = false;
            }
            else
            {
                this.pictureBox1.Image = global::ABC.Properties.Resources.speaker_on;
                this.pictureBox1.Tag = "speaker_on";
                speakerOn = true;
            }
        }



        bool keyOk = false;
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (speekLock)
            {
                return;
            }

            if(e.KeyValue >='A' && e.KeyValue <= 'Z' || e.KeyValue>='0' && e.KeyValue<='9')
            {
                this.label2.Text = Convert.ToChar(e.KeyValue).ToString();
                keyOk = true;
            }
            else if((int)e.KeyCode >= (int)(Keys.NumPad0) && (int)e.KeyCode <= (int)(Keys.NumPad9))
            {
                switch ((int)e.KeyCode)
                {
                    case (int)Keys.NumPad0:
                        this.label2.Text = "0";
                           break;
                    case (int)Keys.NumPad1:
                        this.label2.Text = "1";
                        break;

                    case (int)Keys.NumPad2:
                        this.label2.Text = "2";
                        break;

                    case (int)Keys.NumPad3:
                        this.label2.Text = "3";
                        break;

                    case (int)Keys.NumPad4:
                        this.label2.Text = "4";
                        break;

                    case (int)Keys.NumPad5:
                        this.label2.Text = "5";
                        break;

                    case (int)Keys.NumPad6:
                        this.label2.Text = "6";
                        break;

                    case (int)Keys.NumPad7:
                        this.label2.Text = "7";
                           break;

                    case (int)Keys.NumPad8:
                        this.label2.Text = "8";
                        break;

                    case (int)Keys.NumPad9:
                        this.label2.Text = "9";
                        break;

                }

                keyOk = true;
            }
            else
            {
                keyOk = false;
            }
        }
    }
}
